package com.cskaoyan.servlet;


import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.ConfigService;
import com.cskaoyan.service.ConfigServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/config/*")
public class AdminConfigServlet extends WdBaseServlet {
    ConfigService configService = new ConfigServiceImpl();
    public BaseRespVo order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if("GET".equals(method)){
            //处理get请求

            Map<String, String> map = configService.orderQuery();
            return BaseRespVo.ok(map);
        }else {
            //处理post请求
            String jsonStr = request.getReader().readLine();
            Map<String,String> map = JacksonUtil.read(jsonStr, Map.class);
            configService.orderModify(map);
            return BaseRespVo.ok();
        }
    }
    @SneakyThrows
    public BaseRespVo express(HttpServletRequest request, HttpServletResponse response){
        String method = request.getMethod();
        if("GET".equals(method)){
            //处理get请求

            Map<String, String> map = configService.expressQuery();
            return BaseRespVo.ok(map);
        }else {
            //处理post请求
            String jsonStr = request.getReader().readLine();
            Map<String,String> map = JacksonUtil.read(jsonStr, Map.class);
            configService.expressModify(map);
            return BaseRespVo.ok();
        }
    }
    public BaseRespVo wx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if("GET".equals(method)){
            //处理get请求

            Map<String, Object> map = configService.wxQuery();
            return BaseRespVo.ok(map);
        }else {
            //处理post请求
            String jsonStr = request.getReader().readLine();
            Map<String,Object> map = JacksonUtil.read(jsonStr, Map.class);
            configService.wxModify(map);
            return BaseRespVo.ok();
        }
    }

    public BaseRespVo mall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if("GET".equals(method)){
            //处理get请求

            Map<String, String> map = configService.mallQuery();
            return BaseRespVo.ok(map);
        }else {
            //处理post请求
            String jsonStr = request.getReader().readLine();
            Map<String,String> map = JacksonUtil.read(jsonStr, Map.class);
            configService.mallModify(map);
            return BaseRespVo.ok();
        }

    }
}
